import type { ResponseBody } from '~/utils/request'

export interface LoginParams {
  phone: string
  password?: string
}

export interface LoginMobileParams {
  userPhone: string
  userPassword: string
  verificationcode: string
}

export interface LoginResultModel {
  success: boolean
  message: string
  userName: string | null
  userCompany: string | null
  userPhone: string | null
  token: string | null
}

export function loginAdminApi(params: LoginParams): Promise<ResponseBody<LoginResultModel>> {
  return usePost<LoginResultModel, LoginParams>('/api/v1/admin/login', params, {
    // 设置为false的时候不会携带token
    token: false,
    // 开发模式下使用自定义的接口
    customDev: true,
    // 是否开启全局请求loading
    loading: false,
  })
}



// 发送短信验证码
export interface SendSmsParams {
  phone: string
  ttl_seconds: number
  captcha_id?: string
  captcha_text?: string
}

export interface SendSmsResult {
  phone: string
  ttl_seconds: number
}

export function sendSmsCodeApi(params: SendSmsParams) {
  return usePost<SendSmsResult, SendSmsParams>('/api/v1/auth/sms/send', params, {
    token: false,
    customDev: true,
    loading: false, // 关闭全屏加载蒙版
  })
}

// 手机号 + 验证码 登录
export interface PhoneLoginParams {
  userPhone: string
  userPassword: string
  verificationcode: string
}

export function phoneLoginApi(params: PhoneLoginParams) {
  return usePost<LoginResultModel, PhoneLoginParams>('/api/v1/users/login', params, {
    token: false,
    customDev: true,
    loading: true,
  })
}

// 注册
export interface RegisterParams {
  userName: string
  userCompany: string
  userPassword: string
  confirmPassword: string
  userPhone: string
  verificationcode: string
}

export interface RegisterResult {
  success: boolean
  message: string
  userName: string | null
  userCompany: string | null
  userPhone: string | null
}

export function registerApi(params: RegisterParams): Promise<ResponseBody<RegisterResult>> {
  return usePost<RegisterResult, RegisterParams>('/api/v1/users/register', params, {
    token: false,
    customDev: true,
    loading: true,
  })
}

// 图形验证码接口
export interface CaptchaParams {
  captcha_id?: string
}

export interface CaptchaResult {
  code: number
  message: string
  data: {
    captcha_id: string
    image_base64: string
  }
}

export function getCaptchaImageApi(params?: CaptchaParams) {
  return useGet<CaptchaResult>('/api/v1/auth/captcha/image', params || {}, {
    token: false,
    customDev: true,
    loading: false,
  })
}

// 获取考生详细报名详情信息的LIST接口
export interface ExamSubject {
  id: number
  subject_code: string
  subject_name: string
  description: string
  cover_image?: string
  exam_class: string
  exam_unit: string | null
  exam_fee: string
  single_unit_fee?: string
  two_unit_fee?: string
  status: number
  created_at: string
}

export interface RegistrationItem {
  id: number
  subject_code: string
  exam_subject: ExamSubject
  registration_status: number
  registration_time: string
  exam_fee: string
  paid_amount: string
  payment_status: number
  payment_time: string | null
  payment_method: string | null
  exam_score: string | null
  score_status: number
  audit_remark: string | null
  audit_time: string | null
  makeup_unit: string
  pre_exam_training: number
  username: string
  family_name: string
  given_name: string
  gender: number
  birth_date: string
  phone: string
  landline: string
  email: string
  province: string
  city: string
  county: string
  region_code: string | null
  address: string
  address_en: string
  country_en: string
  company_name: string
  company_name_en: string
  examinee_number: string
  invoice_title: string
  invoice_type: number
  invoice_content: string
  taxpayer_id: string
  title_en: string
  birth_date_en: string
  gender_en: string
  invoice_issue_time: string | null
  payment_proof: string
}

export interface RegistrationListParams {
  current?: number
  pageSize?: number
  [key: string]: any
}

export interface RegistrationListResult {
  code: number
  message: string
  data: RegistrationItem[]
}

export function getRegistrationListApi(params?: RegistrationListParams) {
  return useGet<{ code: number; message: string; data: RegistrationItem[] }, RegistrationListParams>('/api/v1/admin/registrations', params, {
    token: true,
    customDev: true,
    loading: false,
  })
}


// 审核参数
export interface ApproveRegistrationParams {
  id: number
  registration_status: number
  audit_remark?: string
}

// 批量审核参数
export interface BatchApproveParams {
  ids: number[]
  registration_status: number
  audit_remark?: string
}

//  管理员修改 报名审核状态接口
export function updateRegistrationStatusApi(params: ApproveRegistrationParams) {
  return usePut<{ code: number; message: string; data: RegistrationItem }, ApproveRegistrationParams>(`/api/v1/admin/registrations/${params.id}`, params, {
    token: true,
    customDev: true,
    loading: false,
  })
}

// 批量审核接口
export function batchApproveRegistrationApi(params: BatchApproveParams) {
  return usePost<{ code: number; message: string; data: any }, BatchApproveParams>('/api/v1/admin/registrations/batch-approve', params, {
    token: true,
    customDev: true,
    loading: false,
  })
}

// 导出 LIBF 报名记录参数
export interface ExportLibfRegistrationsParams {
  startDate?: string
  endDate?: string
  name?: string
  registration_status?: number
}

// 导出 LIBF 报名记录接口返回类型
export interface ExportLibfRegistrationsResult {
  code: number
  message: string
  data: string
}

// 导出 LIBF 报名记录接口
export function exportLibfRegistrationsApi(params?: ExportLibfRegistrationsParams) {
  return useGet<ExportLibfRegistrationsResult, ExportLibfRegistrationsParams>('/api/v1/admin/registrations/export', params, {
    token: true,
    customDev: true,
    loading: true,
  })
}

// 网站内容项接口
export interface WebsiteContentItem {
  id: number
  channel_id: number
  channel_name?: string
  title: string
  subtitle: string
  summary: string
  content: string
  content_source: string
  author: string
  publish_status: number
  publish_time: string
  is_top: number
  view_count: number
  created_at: string
  updated_at: string
  created_by: string
  cover_image?: string
  cover_url?: string
}

// 获取网站内容列表参数
export interface GetWebsiteContentsParams {
  channel_id?: number
}

// 获取网站内容列表接口返回类型
export interface GetWebsiteContentsResult {
  code: number
  message: string
  data: WebsiteContentItem[]
}

// 获取网站内容列表接口
export function getWebsiteContentsApi(params?: GetWebsiteContentsParams) {
  return useGet<GetWebsiteContentsResult, GetWebsiteContentsParams>('/api/v1/admin/website_contents', params, {
    token: true,
    customDev: true,
    loading: false,
  })
}

// 创建新建文章接口参数
export interface CreateWebsiteContentParams {
  channel_id: number
  title: string
  subtitle: string
  summary: string
  content: string
  content_source: string
  author: string
  publish_status: number // 0-草稿, 1-待审核, 2-已发布, 3-已删除
  publish_time: string // 格式: "2023-01-01 00:00:00"
  is_top: number // 0-否, 1-是
  cover_image?: string
}

// 创建新建文章接口返回类型
export interface CreateWebsiteContentResult {
  code: number
  message: string
  data: WebsiteContentItem
}

// 创建新建文章接口
export function createWebsiteContentApi(params: CreateWebsiteContentParams) {
  return usePost<CreateWebsiteContentResult, CreateWebsiteContentParams>('/api/v1/admin/website_contents', params, {
    token: true,
    customDev: true,
    loading: true,
  })
}

// 注意：获取文章详情需要通过列表接口获取，然后从列表中查找对应的项
// 因为 GET /api/v1/admin/website_contents/{id} 接口不支持（返回 405）

// 更新文章接口参数
export interface UpdateWebsiteContentParams {
  id: number
  channel_id?: number
  title?: string
  subtitle?: string
  summary?: string
  content?: string
  content_source?: string
  author?: string
  publish_status?: number // 0-草稿, 1-待审核, 2-已发布, 3-已删除
  publish_time?: string // 格式: "2023-01-01 00:00:00"
  is_top?: number // 0-否, 1-是
  cover_image?: string
}

// 更新文章接口返回类型
export interface UpdateWebsiteContentResult {
  code: number
  message: string
  data: WebsiteContentItem
}

// 更新文章接口
export function updateWebsiteContentApi(params: UpdateWebsiteContentParams) {
  const { id, ...updateData } = params
  return usePut<UpdateWebsiteContentResult, Omit<UpdateWebsiteContentParams, 'id'>>(`/api/v1/admin/website_contents/${id}`, updateData, {
    token: true,
    customDev: true,
    loading: true,
  })
}

// 通知模板与任务
export interface NotificationTemplateItem {
  id: number
  template_code: string
  template_name: string
  notification_type: number
  subject: string
  content: string
  description: string
  status: number
  created_at: string
  updated_at: string
  created_by: string
}

export interface NotificationTaskCreateParams {
  task_name: string
  template_id: number
  recipient_type: 1 | 2
  send_scope: 1 | 2
  recipient_list?: string | null
  scheduled_time?: string | null
}

export interface NotificationTaskResponse {
  id: number
  task_name: string
  template_id: number
  notification_type: number
  recipient_type: number
  recipient_list: string | null
  send_scope: number
  scheduled_time: string | null
  total_count: number
  success_count: number
  fail_count: number
  created_at: string
  updated_at: string
  created_by: string
}

export function getNotificationTemplatesApi() {
  return useGet<NotificationTemplateItem[]>('/api/v1/admin/notification_templates', {}, {
    token: true,
    customDev: true,
    loading: false,
  })
}

export function createNotificationTaskApi(params: NotificationTaskCreateParams) {
  return usePost<NotificationTaskResponse, NotificationTaskCreateParams>('/api/v1/admin/notification_tasks', params, {
    token: true,
    customDev: true,
    loading: true,
  })
}

// 获取考试科目列表接口返回类型
export interface GetSubjectsResult {
  code: number
  message: string
  data: ExamSubject[]
}

// 获取考试科目列表接口
export function getSubjectsApi() {
  return useGet<GetSubjectsResult>('/api/v1/users/subjects', {}, {
    token: true,
    customDev: true,
    loading: false,
  })
}